package scene;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Scene {
	private List<Modele3D> scene;

	/**
	 * Constructor of a empty scene
	 */
	public Scene() {
		scene = new ArrayList<Modele3D>();
	}

	/**
	 * Constructor of a scene from a given list of modele3D
	 * 
	 * @param newScene
	 *            The list of modele3D
	 */
	public Scene(List<Modele3D> newScene) {
		scene = new ArrayList<Modele3D>(newScene);
	}

	/**
	 * Add a Modele3D to the scene
	 * 
	 * @param mdl
	 *            The Modele3D to be added
	 */
	public void addModele3D(Modele3D mdl) {
		scene.add(mdl);
	}

	/**
	 * Get the list of modele3D constituting the scene
	 * 
	 * @return The list of modele3D
	 */
	public List<Modele3D> getModelList() {
		return this.scene;
	}

	/**
	 * Remove a specified object Modele3D from the scene
	 * 
	 * @param mdl
	 *            The object Modele3D to be removed
	 * @return The index of the Modele3D removed
	 */
	public int removeModel3D(Modele3D mdl) {
		int i = scene.indexOf(mdl);

		if (i == -1) {
			return -1;
		}

		scene.remove(i);
		return i;
	}

	// ..........

	/**
	 * List all the elements "Modele3D" that are components of Scene
	 */
	public void list() {
		System.out.println(scene);
	}

	/**
	 * Get a list of all the Facette3D that made up the scene. This mean get the
	 * Facette3D of all models in the scene
	 * 
	 * @return the list of facette3D
	 */
	public List<Facette3D> getFacettes() {
		List<Facette3D> newList = new ArrayList<Facette3D>();
		Iterator<Modele3D> modelIterator = scene.iterator();
		Iterator<Facette3D> facetteIterator;
		Modele3D newModel;
		while (modelIterator.hasNext()) {
			newModel = modelIterator.next();
			facetteIterator = newModel.getFacettes();
			while (facetteIterator.hasNext()) {
				newList.add(facetteIterator.next());
			}

		}
		return newList;
	}

	/*
	 * main for reasons of test public static void main(String[] args){ Scene sc
	 * = new Scene(); Modele3D cube1 = new Cube(10, new Point(0,0,0)); Modele3D
	 * cube2 = new Cube(20, new Point(1,3,4)); sc.addModele3D(cube1);
	 * sc.addModele3D(cube2); sc.list(); System.out.println();
	 * System.out.println(sc.getFacettes()); sc.removeModel3D(cube1); sc.list();
	 * }
	 */

	/**
	 * This method calculates the altitude of a given list of Facette3D
	 * representing a scene.
	 * 
	 * @param facetteList
	 *            the list of Facette3D
	 * @return a list of Facette3D with the altitudes calculated.
	 */
	public static List<Facette3D> altitude(List<Facette3D> facetteList) {
		Facette3D newFacette;
		ListIterator<Facette3D> facetteIterator = facetteList.listIterator();
		while (facetteIterator.hasNext()) {
			newFacette = facetteIterator.next();
			newFacette.setAltitude();
			facetteIterator.set(newFacette);
		}
		return facetteList;
	}

}
